'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, TEXT, TINYINT, DATE } = Sequelize;
    
    await queryInterface.createTable('customers', {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: {
        type: STRING(200),
        allowNull: false,
        comment: '客户名称'
      },
      code: {
        type: STRING(100),
        comment: '客户编码'
      },
      contact_persons: {
        type: TEXT,
        comment: '联系人信息[{"name":"","phone":"","role":""}]'
      },
      status: {
        type: TINYINT,
        defaultValue: 1,
        comment: '状态:1启用,0禁用'
      },
      created_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // 添加索引
    await queryInterface.addIndex('customers', ['name'], {
      name: 'customers_idx_name'
    });
    
    await queryInterface.addIndex('customers', ['status'], {
      name: 'customers_idx_status'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('customers');
  }
};